package data;

import java.sql.*;

import sql.SqlExecutor;

public class Events {
	public int id=0;
	public String name="";
	
	public Events(int _id,String _name){
		id = _id;
		name = _name;
	}
	
	public static Events[] fromResultSet(ResultSet rs){
		try {
			rs.last();
			int count=rs.getRow();
			int index=0;
			Events[] events=new Events[count];
			rs.beforeFirst();
			while(rs.next()){
				events[index]=new Events((Integer)rs.getObject(1),(String)rs.getObject(2));
				index++;
			}			
			return events;
		}
		catch (SQLException e){
			e.printStackTrace();
			return new Events[0];
		}		
	}
	
	public static int getEventIdByName(String eventName){
		System.out.println("eventName="+eventName);
		String sql="select id,name from Events where name=\""+eventName+"\"";
		ResultSet rs=SqlExecutor.executeQuery(sql);
		Events[] e=Events.fromResultSet(rs);
		SqlExecutor.closeResultSet(rs);
		if(e.length==0){
			return -1;
		}
		return e[0].id;
	}
	
	public static String getEventNameById(int eventId){
		String sql="select id,name from Events where id="+eventId;
		ResultSet rs=SqlExecutor.executeQuery(sql);
		Events[] e=Events.fromResultSet(rs);
		SqlExecutor.closeResultSet(rs);
		if(e.length==0){
			return "";
		}
		return e[0].name;
	}
}
